package com.xuanyuan.adapter

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.xuanyuan.baselibaray.ItemClickListener
import com.xuanyuan.material.R
import com.xuanyuan.material.databinding.ItemMainListBinding

/**
 * 主页列表
 *
 * @author ken.luo
 */
class MainListAdapter : RecyclerView.Adapter<MainListAdapter.ViewHolder>() {
    private var mList: List<JumpGoBean>?

    private var clickListener: ItemClickListener<JumpGoBean>? = null


    @JvmName("setClickListener1")
    fun setClickListener(clickListener: ItemClickListener<JumpGoBean>) {
        this.clickListener = clickListener
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val binding = DataBindingUtil.inflate<ItemMainListBinding>(
            LayoutInflater.from(parent.context),
            R.layout.item_main_list,
            parent, false
        )
        return ViewHolder(binding)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        mList?.get(position)?.let { bean ->
            holder.mItemBinding.bean = bean
            holder.mItemBinding.itemContent.setOnClickListener {
                clickListener?.onItemClick(position, bean)
            }
        }
    }

    override fun getItemCount(): Int {
        return if (mList == null) {
            0
        } else {
            mList!!.size
        }
    }

    @SuppressLint("NotifyDataSetChanged")
    fun setList(list: List<JumpGoBean>?) {
        mList = list
        notifyDataSetChanged()
    }

    class ViewHolder(var mItemBinding: ItemMainListBinding) :
        RecyclerView.ViewHolder(mItemBinding.itemContent)

    init {
        mList = emptyList()
    }
}